/**
 * @file board.h
 * @author fwy (2643660853@qq.com)
 * @brief
 * @version 0.1
 * @date 2025-08-18
 *
 * @copyright Copyright (c) 2025
 *
 */
#pragma once
#include "socket_client.h"
#include "serial.h"

#include <memory>
#include <atomic>
#include <thread>

#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QLabel>
#include <QWidget>
#include "admin_dialog.h"
#include "info_dialog.h"
#include "cabinet_dialog.h"
#include "cabinet_collect_dialog.h"
#include "cabinet_halfway_dialog.h"
#include "cabinet_return_dialog.h"
#include "http_client.h"
struct Lock
{
    std::string lock;
    std::string zone;
    std::string lock_numble;
    std::string status;
    std::string brand_code;
    std::string organization;
    std::string controller_id;
    std::string phone;
    std::string person;
    std::string collect_time;
    std::string use_time;
    std::string type;
    std::string layer;
    std::string cabinet_number;
    std::string effective_time;
};
class Board
{
public:
    Board();
    ~Board() = default;
    bool processUI(int argc, char *argv[]);
    void handleAdmin(QWidget *window);
    void handleStore(QWidget *window);
    void handleHalfway(QWidget *window);
    void handleReturn(QWidget *window);
    void handleLongRent(QWidget *window);
    Lock httpDistributeLock(const std::string &brand_code, const std::string &organization,
                            const std::string &zone, const std::string &phone, const std::string &type, const std::string &layer);
    std::string httpGetPersonPasswd(const std::string &brand_code, const std::string &organization,
                                    const std::string &zone, const std::string &phone);
    Lock httpQueryLock(const std::string &brand_code, const std::string &organization,
                       const std::string &zone, const std::string &phone, const std::string &type);

    Lock httpReturnLock(const std::string &brand_code, const std::string &organization,
                        const std::string &zone, const std::string &phone, const std::string &type);
    int httpGetAmountCabinetByLayer(const std::string &brand_code, const std::string &organization,
                                    const std::string &zone, const std::string &layer);
    void httpUploadLog(const std::string &brand_code, const std::string &organization,
                       const std::string &zone, const std::string &phone, const std::string &cabinet_number,
                       const std::string &cabinet_type, const std::string &action);
    std::vector<Lock> httpGetAllLocks(const std::string &brand_code, const std::string &organization,
                                    const std::string &zone);
    bool loadDeviceConfig();

    inline void showBigMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text)
    {
        QMessageBox msgBox(icon, title, text, QMessageBox::Ok, parent);
        msgBox.setStyleSheet("QLabel{font-size:48px;} QPushButton{font-size:40px; min-width:180px; min-height:80px;}");
        msgBox.setMinimumSize(800, 800);
        msgBox.exec();
    }

private:
    std::unique_ptr<SocketClient> socket_client_;
    std::unique_ptr<Serial> serial_;
    std::unique_ptr<HttpClient> http_client_;

    struct
    {
        QString brand_code;   // 品牌代码
        QString organization; // 门店名称
        QString zone;         // 管理区
        QString sn_code;      // SN码
        QString device_type;  // 设备类型
    } deviceConfig;
};